package java6699.register;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;

/**
 * @program: java_zzx
 * @description:
 * @author: zzx
 * @create: 2022-11-09 15:53
 **/
public class Register6699Controller {
    @FXML
    public TextField tfName;
    @FXML
    public PasswordField pf1;
    @FXML
    public PasswordField pf2;
    @FXML
    public TextField tfPhone;
    @FXML
    public Label lblName;
    @FXML
    public Label lblPW1;
    @FXML
    public Label lblPW2;
    @FXML
    public Label lblPhone;
    @FXML
    public Button btnOk;
    @FXML
    public ListView<User6699> lvUsers;
    @FXML
    public Button btnNO;

    @FXML
    public void initialize(){
        tfName.focusedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                checkUserName();
            }
        });
        pf1.focusedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                checkPw1();
            }
        });
        pf2.focusedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                checkPw2();
            }
        });
        tfPhone.focusedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                checkPhone();
            }
        });
    }

    @FXML
    public void enterKey(ActionEvent actionEvent) {
        if (tfName.isFocused()) {
               checkUserName();
            if (lblName.isVisible()) {
                lblPW1.requestFocus();//焦点跳至lblPW1
            }
        } else if(pf1.isFocused()){//判断焦点是否在pf1
            checkPw1();
            if(lblPW1.isVisible()){
                lblPW2.requestFocus();
            }
        } else if(pf2.isFocused()){//判断焦点是否在pf2
            checkPw2();
            if(lblPW2.isVisible()){
                tfPhone.requestFocus();
            }
        } else if(tfPhone.isFocused()){//判断焦点是否在tfPhone
            checkPhone();
            if(lblPhone.isVisible()){
                btnOk.requestFocus();
            }
        }
    }


    @FXML
    public void okClick(ActionEvent actionEvent) {
        checkUserName();checkPw1();checkPw2();checkPhone();
        if(!lblPhone.isVisible()&&!lblPW1.isVisible()&&!lblName.isVisible()&&!lblPW2.isVisible()){
            btnOk.setDefaultButton(true);
            lvUsers.getItems().add(new User6699(tfName.getText(),pf1.getText(), tfPhone.getText()));
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("用户注册V1.0|集合版");
            alert.setHeaderText("恭喜用户"+tfName.getText());
            alert.setContentText("注册成功!");
            alert.show();
            noClick(null);//清空输入框内容
        }
    }

    @FXML
    public void noClick(ActionEvent actionEvent) {
        btnNO.setDefaultButton(true);
        tfName.setText("");
        pf1.setText("");
        pf2.setText("");
        tfPhone.setText("");
        lblName.setVisible(false);
        lblPW1.setVisible(false);
        lblPW2.setVisible(false);
        lblPhone.setVisible(false);
        tfName.requestFocus();
    }
    public void checkUserName(){
        String username = tfName.getText();
        if (username.equals("") || username.matches("\\s+")) {
            lblName.setVisible(true);
        }else if(!username.matches("\\s+")){
            int count = 0;
            for (User6699 u : lvUsers.getItems()) {
                //用户名不区分大小写
                if (u.getUsername().equalsIgnoreCase(username)) {
                    lblName.setVisible(true);
                }else {
                    count++;
                }
            }
            if (count == lvUsers.getItems().size()){
                lblName.setVisible(false);
            }
        }
    }
    public void checkPw1(){
        lblPW1.setVisible(!pf1.getText().matches("(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{6,}"));
    }

    private void checkPw2() {
        lblPW2.setVisible(!pf2.getText().equals(pf1.getText()) || pf2.getText().equals(""));
    }

    private void checkPhone() {
        lblPhone.setVisible(!tfPhone.getText().matches("1[3578]\\d{9}"));
    }
}
